package main

import (
	"context"
	"fmt"
	"golang.org/x/time/rate"
	"log"
	"time"
)

func main() {
	const (
		speed = 1  //每秒执行的次数
		capacity = 5 //桶的容量大小
	)
	var gameScene = rate.NewLimiter(speed , capacity)
	var ctx = context.Background()

	// 限流一
	for {
		err := gameScene.WaitN(ctx, 2)
		if err != nil {
			log.Fatal(err)
		}
		// 业务代码
		fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
		time.Sleep(time.Second)
	}
}
